/**
 * 数据字典项
 * @param {Object} main
 */
Ext.extend(Bee.module, {
    init: function(main){
        var dic = new Bee.form.DWRComboBox({
            fields: ['sid', 'cname'],
            valueField: 'sid',
            displayField: 'cname',
            ds: {
                call: smDwr.findDictionary,
                params: {
                    eq_status: 1,
                    asc_sort: 1
                }
            },
            listeners: {
                render: function(o){
                    o.store.on('load', function(p){
                        if (p.getCount() > 0) {
                            o.setValue(p.getAt(0).data.sid);
                        }
                    });
                    o.store.reload();
                }
            }
        });
        
        var searchBtn = new Ext.Button({
            iconCls: 'btn_search',
            text: '查询',
            handler: function(){
                grid.reload();
            }
        });
        
        var tbar = ['数据字典:', dic, '-', searchBtn];
        
        var grid = new Bee.grid.PageGrid({
            id: 'sm-dictionaryItemMan-grid',
            region: 'center',
            initload: false,
            firstColumn: 'radio',
            filter: {
                eq_status: 1,
                asc_sort: 1
            },
            dwr: {
                call: smDwr.pageDictionaryItem
            },
            tbar: tbar,
            metas: [{
                id: 'sid',
                field: 'sid',
                type: 'int',
                hidden: true
            }, {
                field: 'cname',
                desc: '名称'
            }, {
                field: 'val',
                desc: '值'
            }, {
                field: 'sort',
                desc: '排序'
            }],
            listeners: {
                loadcheck: function(){
                    delete grid.filter.eq_smDictionarySid;
                    grid.filter.eq_smDictionarySid = dic.getValue();
                },
                render: function(o){
                    o.getSelectionModel().on('selectionchange', function(p){
                        if (p.getCount() === 0) {
                            infoForm.getForm().reset();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-saveBtn').enable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-updateBtn').disable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-resetBtn').enable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-deleteBtn').disable();
                        }
                        if (p.getCount() === 1) {
                            infoForm.load(o.getSelectedRow().sid);
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-saveBtn').disable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-updateBtn').enable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-resetBtn').enable();
                            Ext.getCmp('sm-dictionaryItemMan-infoForm-deleteBtn').enable();
                        }
                    });
                }
            }
        });
        
        var infoForm = new Bee.form.DWRForm({
            id: 'sm-dictionaryItemMan-infoForm',
            region: 'east',
            cls: 'x-bee-form-left',
            width: 400,
            labelWidth: 80,
            action: 'save',
            dwr: {
                save: smDwr.saveDictionaryItem,
                update: smDwr.updateDictionaryItem,
                load: smDwr.getDictionaryItem
            },
            afterSubmit: function(){
                grid.reload();
                infoForm.getForm().reset();
            },
            items: [{
                fieldLabel: 'sid',
                name: 'sid',
                xtype: 'hidden'
            }, {
                fieldLabel: 'status',
                name: 'status',
                xtype: 'hidden',
				value: 1
            }, {
                fieldLabel: 'smOrganizationSid',
                name: 'smOrganizationSid',
                xtype: 'hidden'
            }, {
                fieldLabel: 'smDictionarySid',
                name: 'smDictionarySid',
                xtype: 'hidden'
            }, {
                fieldLabel: '名称',
                name: 'cname',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '值',
                name: 'val',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '排序',
                name: 'sort',
                xtype: 'numberfield',
                allowDecimals: false,
                allowNegative: false,
                allowBlank: false,
                maxLength: 3
            }, {
                xtype: 'panel',
                layout: 'hbox',
                bodyCssClass: 'x-bee-form-hbox',
                layoutConfig: {
                    padding: '5',
                    pack: 'center',
                    align: 'middle'
                },
                defaults: {
                    xtype: 'button',
                    minWidth: 75,
                    hidden: false,
                    margins: '0 10 0 0'
                },
                items: [{
                    id: 'sm-dictionaryItemMan-infoForm-saveBtn',
                    text: '新增',
                    iconCls: 'btn_insert',
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm.getForm().setValues({
                                    smDictionarySid: dic.getValue()
                                });
                                infoForm['save']();
                            }
                        });
                    }
                }, {
                    id: 'sm-dictionaryItemMan-infoForm-updateBtn',
                    text: '修改',
                    iconCls: 'btn_modify',
                    disabled: true,
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm['update']();
                            }
                        });
                    }
                }, {
                    id: 'sm-dictionaryItemMan-infoForm-resetBtn',
                    text: '刷新',
                    iconCls: 'btn_refresh',
                    handler: function(){
                        if (!grid.getSelectedRow()) {
                            infoForm.getForm().reset();
                        }
                        else {
                            infoForm.load(grid.getSelectedRow().sid);
                        }
                    }
                }, {
                    id: 'sm-dictionaryItemMan-infoForm-deleteBtn',
                    text: '删除',
                    iconCls: 'btn_delete',
                    disabled: true,
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认删除吗?', function(param){
                            if (param == 'yes') {
                                smDwr.deleteDictionaryItem(grid.getSelectedRow().sid, function(){
                                    Bee.msg('删除成功!');
                                    grid.reload();
                                });
                            }
                        });
                    }
                }]
            }]
        });
        
        var panel = new Ext.Panel({
            id: 'sm-dictionaryItemMan-panel',
            layout: 'border',
            border: false,
            items: [grid, infoForm],
            loadFlag: true
        });
        
        this.main.associatedComp = [];
        this.main.add(panel);
        this.main.doLayout();
    }
});
